package com.tjp.eight.ui.fragment.otherhomepage;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tjp.eight.R;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.AppUtils;
import com.tjp.eight.utils.BitmapUtils;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.views.emoj.EmojiUtil;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/24.
 */
public class OtherHomePageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private String fromName = "青春";
    private String toName = "Mr.周";
    private NameOnClick nameOnClick;
    private ArrayList<OtherHomeRModel.OtherHomeItem> models;

    public OtherHomePageAdapter(Context context) {
        this.context = context;
        models = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OtherHomePageViewHolder(LayoutInflater.from(context).inflate(R.layout.item_my_page_action, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final String cid = models.get(position).contentid;
        ((OtherHomePageViewHolder) holder).tvComment.setText(models.get(position).detail);
        if (StringUtils.isNullOrEmpty(models.get(position).detail)) {
            ((OtherHomePageViewHolder) holder).tvComment.setVisibility(View.GONE);
        } else {
            ((OtherHomePageViewHolder) holder).tvComment.setVisibility(View.VISIBLE);
        }
        ((OtherHomePageViewHolder) holder).tv_pass_time.setText("" + models.get(position).pass_time);
        ((OtherHomePageViewHolder) holder).tv_title.setText("" + models.get(position).title);

        ((OtherHomePageViewHolder) holder).rl_detail_a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.startDetails(context, cid);
            }
        });
        setImg(((OtherHomePageViewHolder) holder).iv_type, models.get(position).icon_type);
        Glide.with(context).load(models.get(position).cover_path).error(R.drawable.icon_fail).into(((OtherHomePageViewHolder) holder).iv_my_p_action_pic);
        ((OtherHomePageViewHolder) holder).rl_detail_a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.startDetails(context, cid);
            }
        });
        ArrayList<String> names = AppUtils.getNames(models.get(position).detail);
        ArrayList<String> uids = AppUtils.getUids(models.get(position).detail);
        String[] split = AppUtils.getSplit(models.get(position).detail);
        if (names != null && !names.isEmpty())
            AppUtils.getDetail(new Clickable(""), new Clickable(""), split, names, ((OtherHomePageViewHolder) holder).tvComment, context);
        else {
            try {
                new EmojiUtil().handlerEmojiText(((OtherHomePageViewHolder) holder).tvComment, models.get(position).detail, context);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    void setImg(ImageView view, int type) {
        switch (type) {
            case 1://icon_type {"1":"阅读","2":"评论","3":"分享","4":"赞别人的评论","5":"赞别人的回复","6":"回复"}
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_read));
                break;
            case 2:
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_comment));
                break;
            case 3:
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_share));
                break;
            case 4:
               // view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_zan_o));
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_collect));
                break;
            case 5:
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_zan_o_r));
                break;
            case 6:
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_reply));
                break;
        }
    }

    public void setNameOnClick(NameOnClick nameOnClick) {
        this.nameOnClick = nameOnClick;
    }


    @Override
    public int getItemCount() {
        return models.size();
    }

    public void addData(ArrayList<OtherHomeRModel.OtherHomeItem> listdata) {
        if (listdata == null || listdata.isEmpty()) return;
        int index = models.size() - 1;
        if (index < 0) {
            index = 0;
        }
        for (OtherHomeRModel.OtherHomeItem model : listdata) {
            models.add(model);
        }
        notifyItemRangeInserted(index, getItemCount());
    }

    protected interface NameOnClick {
        void onClick(String name);
    }

    private class OtherHomePageViewHolder extends RecyclerView.ViewHolder {
        public OtherHomePageViewHolder(View view) {
            super(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            tvComment = (TextView) view.findViewById(R.id.tv_my_page_comment);
            tv_pass_time = (TextView) view.findViewById(R.id.tv_pass_time);
            tv_title = (TextView) view.findViewById(R.id.tv_title);
            iv_my_p_action_pic = (ImageView) view.findViewById(R.id.iv_my_p_action_pic);
            iv_type = (ImageView) view.findViewById(R.id.iv_type);
            rl_detail_a = (RelativeLayout) view.findViewById(R.id.rl_detail_a);
        }

        TextView tvComment;
        TextView tv_pass_time;
        TextView tv_title;
        ImageView iv_my_p_action_pic;
        ImageView iv_type;
        RelativeLayout rl_detail_a;

    }

    class Clickable extends ClickableSpan {
        private String name;

        public Clickable(String s) {
            name = s;
        }

        /**
         * 重写父类点击事件
         */
        @Override
        public void onClick(View v) {
            if (nameOnClick != null)
                nameOnClick.onClick(name);
        }

        /**
         * 重写父类updateDrawState方法  我们可以给TextView设置字体颜色,背景颜色等等...
         */
        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.parseColor("#0BBBF8"));
        }
    }
}
